﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GMusicBox.Core;
using GMusicBox.Core.Setting;

namespace GMusicBox
{
    public partial class frmEQ : Form
    {
        public frmEQ(BassEngine be)
        {
            InitializeComponent();

            this.be = be;
            btnEnabled.Text = "启用";
            btnReset.Text = "重置";
            btnDefault.Text = "预设";
            Initialize();
        }

        BassEngine be;
        bool enabled = false;

        private void Initialize()
        {
            if (FXSetting.Enabled) btnEnabled_Click(btnEnabled, null);
            for (int i = 0; i < 10; i++)
                ((TrackBar)flowLayoutPanel1.Controls["t" + i.ToString()]).Value = FXSetting.Equalizers[i];
            tReverb.Value = FXSetting.ReverbTime;
            tEquilibrium.Value = FXSetting.Equilibrium;
            tPreamp.Value = FXSetting.PreampGain;
        }
        
        private void tScroll(object sender, EventArgs e)
        {
            TrackBar send = (TrackBar)sender;
            if (send.Tag == null || (send.Tag != null && (double)send.Tag != send.Value))
            {
                toolTip1.Show(send.Value.ToString(), send, send.Width + 2, send.Height - (int)((send.Value - send.Minimum) / (double)(send.Maximum - send.Minimum) * (send.Height - 14)) - 16);
                send.Refresh();
                send.Tag = send.Value;
            }
        }

        private void tScrollEnd(object sender, EventArgs e)
        {
            toolTip1.Hide((IWin32Window)sender);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control item in flowLayoutPanel1.Controls)
                ((TrackBar)item).Value = 0;
            tEquilibrium.Value = 0;
            tReverb.Value = 0;
            tPreamp.Value = 0;
        }

        private void tEquilibrium_Scroll(object sender, EventArgs e)
        {
            if (tEquilibrium.Tag == null || (tEquilibrium.Tag != null && (double)tEquilibrium.Tag != tEquilibrium.Value))
            {
                string s = "{0} {1}%";
                if (Math.Round(tEquilibrium.Value, 1) == 0) s = "居中";
                else if (tEquilibrium.Value < 0) s = string.Format(s, "偏左", Math.Round(-tEquilibrium.Value, 1) * 100);
                else s = string.Format(s, "偏右", Math.Round(tEquilibrium.Value, 1) * 100);

                toolTip1.Show(s, tEquilibrium, tEquilibrium.Width, -4);
                tEquilibrium.Refresh();
                tEquilibrium.Tag = tEquilibrium.Value;
            }
        }

        private void tReverb_Scroll(object sender, EventArgs e)
        {
            if (tReverb.Tag == null || (tReverb.Tag != null && (double)tReverb.Tag != tReverb.Value))
            {
                string s = tReverb.Value == 0 ? "关闭" : ((int)tReverb.Value).ToString();
                toolTip1.Show(s, tReverb, tReverb.Width, -4);
                tReverb.Refresh();
                tReverb.Tag = tReverb.Value;
            }
        }

        private void tValueChanged(object sender, EventArgs e)
        {
            if (!enabled) return;

            TrackBar send = (TrackBar)sender;
            int index = int.Parse(send.Name.Substring(1));
            be.SetEqualizer(index, (int)send.Value);
            FXSetting.UpdateEqualizer(index, (int)send.Value); FXSetting.Save();

            int i = GetCurrentEQ();
            if (i != -1) btnDefault.Text = contextMenu1.MenuItems[i].Text;
            else btnDefault.Text = "预设";
        }

        private void frmEQ_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void btnEnabled_Click(object sender, EventArgs e)
        {
            if (enabled)
            {
                be.IsEqualizerEnabled = false;
                be.IsReverbEnabled = false;
                be.IsPreampEnabled = false;
                be.Equilibrium = 0;

                tEquilibrium.Enabled = false;
                tPreamp.Enabled = false;
                tReverb.Enabled = false;
                flowLayoutPanel1.Enabled = false;
                btnDefault.Enabled = false;
                btnReset.Enabled = false;

                enabled = false;
                FXSetting.Enabled = false; FXSetting.Save();
                btnEnabled.Text = "启用";
            }
            else
            {
                tEquilibrium.Enabled = true;
                tPreamp.Enabled = true;
                tReverb.Enabled = true;
                flowLayoutPanel1.Enabled = true;
                btnDefault.Enabled = true;
                btnReset.Enabled = true;

                be.IsEqualizerEnabled = true;
                for (int i = 0; i < 10; i++)
                {
                    TrackBar t = ((TrackBar)flowLayoutPanel1.Controls["t" + i.ToString()]);
                    t.Value = t.Value;
                }

                if (tReverb.Value != 0)
                {
                    be.IsReverbEnabled = true;
                    be.SetReverbTime((int)tReverb.Value);
                }

                if (tPreamp.Value != 0)
                {
                    be.IsPreampEnabled = true;
                    be.SetPreampGain((int)tPreamp.Value);
                }

                be.Equilibrium = tEquilibrium.Value;

                int index = GetCurrentEQ();
                if (index != -1) btnDefault.Text = contextMenu1.MenuItems[index].Text;
                else btnDefault.Text = "预设";

                enabled = true;
                FXSetting.Enabled = true; FXSetting.Save();
                btnEnabled.Text = "禁用";
            }
        }

        private void tReverb_ValueChanged(object sender, EventArgs e)
        {
            if (!enabled) return;
            
            if ((int)tReverb.Value == 0) be.IsReverbEnabled = false;
            else
            {
                if (!be.IsReverbEnabled) be.IsReverbEnabled = true;
                be.SetReverbTime((int)tReverb.Value);
            }

            FXSetting.ReverbTime = (int)tReverb.Value; FXSetting.Save();
        }

        private void tEquilibrium_ValueChanged(object sender, EventArgs e)
        {
            if (!enabled) return;

            be.Equilibrium = tEquilibrium.Value;
            FXSetting.Equilibrium = tEquilibrium.Value; FXSetting.Save();
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            contextMenu1.Show(btnDefault, new Point(0, btnDefault.Height));
        }

        private void contextMenu1_Popup(object sender, EventArgs e)
        {
            int i = GetCurrentEQ();
            foreach (MenuItem item in contextMenu1.MenuItems)
                item.Checked = false;
            if (i != -1) contextMenu1.MenuItems[i].Checked = true;
        }

        private int GetCurrentEQ()
        {
            int i = 0;
            for (i = 0; i < BassEngine.DefaultEqualizers.Length; i++)
            {
                int count = 0;
                for (int j = 0; j < 10; j++)
                {
                    if (((TrackBar)flowLayoutPanel1.Controls["t" + j.ToString()]).Value == BassEngine.DefaultEqualizers[i][j])
                        count++;
                }
                if (count == 10) break;
            }

            if (i == BassEngine.DefaultEqualizers.Length) return -1;
            else return i;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
                ((TrackBar)flowLayoutPanel1.Controls["t" + i.ToString()]).Value = BassEngine.DefaultEqualizers[((MenuItem)sender).Index][i];
        }

        private void tPreamp_ValueChanged(object sender, EventArgs e)
        {
            if (!enabled) return;

            if ((int)tPreamp.Value == 0) be.IsPreampEnabled = false;
            else
            {
                if (!be.IsPreampEnabled) be.IsPreampEnabled = true;
                be.SetPreampGain((int)tPreamp.Value);
            }

            FXSetting.PreampGain = (int)tPreamp.Value; FXSetting.Save();
        }
    }
}
